function [estimatedFixedPoint] = fixedPointIteration(func, initFixedPoint, tol, maxIter)
%FIXEDPOINTITERATION Function that determines the fixed point p=g(p) given
%an initial approximation using the Fixed-Point Iteration method.
%   This algorithm was adapted from Chapter 2.2 of Numerical Analysis (8th
%   edition, Burden & Faires)
%   
%   *Input Parameters*
%   func: String containing name of function to be evaluated to find root.
%   initFixedPoint: Initial approximation for the fixed point of func
%   tol: Tolerance limit (evaluated as the absolute difference between the
%        estimated fixed point between two iteration)
%   maxIter: Maximum number of iterations
%
%   *Default options* 
%   tol = 10^-5
%   maxIter = 100
%
%
%   ***If no fixed point is found after maxIteration, the midpoint for 
%   maxIter approximation is returned.***
%
%   ***********************************************************************
%   Author: Mathieu Boudreau, BSc, MSc, PhD Candidate (BME)
%   Institute: Montreal Neurological Institute, McGill University
%   Contact: mathieu.boudreau2 (at) mail.mcgill.ca
%   Date: July 16th 2014
%   ***********************************************************************


%% Set default input conditions, if required.
%

if nargin < 4
    maxIter=100;
end

if nargin < 3
    tol=10^-5;
end

%% Initialize initial conditions
%
initIter=1;

%% Run Bisection Algorithm
%
for iter=initIter:maxIter
    estimatedFixedPoint = feval(func,initFixedPoint);
    if abs(estimatedFixedPoint-initFixedPoint)<tol
        disp(['Converged after ' num2str(iter) ' iterations'])
        return
    end
    initFixedPoint=estimatedFixedPoint;
end

if iter==maxIter
    disp('Maximum number of iteration reached without convergence. Output is estimate after maximum number of iterations.')
end

end
